<template>
  <div class="register">
    <el-main>
      <el-form
        ref="registerformref"
        :model="registerform"
        label-position="top"
        :rules="registerrules"
      >
        <h3>新用户注册</h3>
        <el-form-item :label-width="formLabelWidth" prop="username">
          <el-input
            v-model="registerform.username"
            type="text"
            placeholder="输入用户名"
          />
        </el-form-item>
        <el-form-item :label-width="formLabelWidth" prop="password">
          <el-input
            v-model="registerform.password"
            type="password"
            placeholder="输入密码"
            show-password
          />
        </el-form-item>
        <el-form-item :label-width="formLabelWidth" prop="usertype">
          <el-select
            v-model="registerform.usertype"
            class="selects"
            placeholder="选择类型"
            clearable
          >
            <el-option
              v-for="item in options"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            />
          </el-select>
        </el-form-item>

        <!-- <el-col :span="5">
                    <el-button type="primary" @click="back" :loading="logining">返回</el-button>
                </el-col>
                <el-col :span="4">
                    <el-button type="primary" @click="register" :loading="logining">注册</el-button>
                </el-col> -->
      </el-form>
      <span style="margin-left: 60%">
        <el-button type="primary" @click="back" :loading="logining"
          >返回</el-button
        >
        <el-button
          type="primary"
          @click="register(registerformref)"
          :loading="logining"
          >注册</el-button
        >
      </span>
    </el-main>
  </div>
</template>
<script>
import { ref } from "vue";
import { useRouter } from "vue-router";
import { userregister } from "@/api";
import { ElMessage } from "element-plus";
import { titleTest } from "../utils/checkrules";
export default {
  name: "UserRegister",
  setup() {
    let load = ref({
      state: false,
    });

    const router = useRouter();

    const registerform = ref({
      username: "",
      password: "",
      usertype: "",
    });

    const options = ref([
      {
        value: "1",
        label: "管理员",
      },
      {
        value: "2",
        label: "普通用户",
      },
    ]);

    let registerformref = ref(null);

    const registerrules = ref({
      username: [
        { required: true, message: "用户名称为必填项", trigger: "blur" },
        { max: 5, message: "最多5个字符", trigger: "blur" },
        { validator: titleTest, trigger: "blur" },
      ],
      password: [
        { required: true, message: "密码为必填项", trigger: "blur" },
        { max: 16, message: "最多16个字符", trigger: "blur" },
        { min: 6, message: "最少6个字符", trigger: "blur" },
        { validator: titleTest, trigger: "blur" },
      ],
      usertype: [
        { required: true, message: "用户类型为必选项", trigger: "blur" },
      ],
    });
    function back() {
      router.push({
        name: "MyLogin",
      });
    }
    function register(refs) {
      console.log(refs);
      refs.validate((valid) => {
        if (valid) {
          userregister(
            registerform.value.username,
            registerform.value.password,
            registerform.value.usertype
          ).then((res) => {
            if (res.data.code == 0) {
              load.value.state = false;
              ElMessage({
                message: res.data.msg,
                type: "success",
                duration: 1000,
              });
              back();
            } else {
              load.value.state = false;
              ElMessage({
                message: res.data.msg,
                type: "error",
                duration: 1000,
              });
            }
          });
          console.log(registerform.value.usertype);
        }
      });
    }

    return {
      registerform,
      options,
      register,
      back,
      load,
      registerrules,
      registerformref,
    };
  },
};
</script>
<style>
.register {
  width: 20%;
  height: 30%;
  margin: auto;
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  border-style: solid;
  border-width: 1px;
  border-color: #909399;
  border-radius: 5px;
  line-height: 90%;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.12), 0 0 6px rgba(0, 0, 0, 0.04);
}
.el-row {
  margin-bottom: 20px;
}
.selects {
  width: 360px;
}
</style>
